package cn.ranko.shiro.controller;

import cn.ranko.bean.Datagrid;
import cn.ranko.bean.Pagination;
import cn.ranko.shiro.bean.authority.Message;
import cn.ranko.shiro.constants.ApplicationCode;
import cn.ranko.shiro.model.SysPicklist;
import cn.ranko.shiro.model.SysPicklistvalue;
import cn.ranko.shiro.service.SysPicklistService;
import cn.ranko.shiro.service.SysPicklistvalueService;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by user on 2017/8/30.
 */
@Controller
@RequestMapping("/shiro/picklist")
public class PicklistController {

    @Autowired
    SysPicklistService sysPicklistService;

    @Autowired
    SysPicklistvalueService sysPicklistvalueService;

    @RequiresPermissions("picklist")
    @RequestMapping("/index.action")
    public String index(ModelMap modelMap){
        return "picklist/index";
    }

    @RequiresPermissions("picklist")
    @RequestMapping("/loadList.action")
    @ResponseBody
    public Datagrid loadList(Integer page, Integer rows){
        Pagination pagination = new Pagination();
        pagination.setPage(page);
        pagination.setRows(rows);
        return sysPicklistService.loadList(pagination);
    }

    @RequiresPermissions(value = {"picklist_add","picklist_edit"},logical = Logical.OR)
    @RequestMapping("/showedit.action")
    public String showedit(Integer picklist_id,ModelMap modelMap){
        if(picklist_id==0){
            //添加
        }else{
            //修改
            SysPicklist sysPicklist = sysPicklistService.getByPicklistId(picklist_id);
            modelMap.addAttribute("sysPicklist",sysPicklist);

            List<SysPicklistvalue> sysPicklistvalues = sysPicklistvalueService.getByPicklistId(picklist_id);
            modelMap.addAttribute("sysPicklistvalues",sysPicklistvalues);
        }
        return "picklist/showedit";
    }

    /**
     * 字典查看
     *
     * @return
     */
    @RequestMapping("/view.action")
    public String view(Integer picklist_id,ModelMap modelMap){

        SysPicklist sysPicklist = sysPicklistService.getByPicklistId(picklist_id);
        modelMap.addAttribute("sysPicklist",sysPicklist);

        List<SysPicklistvalue> sysPicklistvalues = sysPicklistvalueService.getByPicklistId(picklist_id);
        modelMap.addAttribute("sysPicklistvalues",sysPicklistvalues);

        return "picklist/view";
    }

    /**
     * 字典保存
     *
     * @return
     */
    @RequiresPermissions(value = {"picklist_add","picklist_edit"},logical = Logical.OR)
    @RequestMapping("/save.action")
    public String save(SysPicklist picklist,String picklistvalues){
        if(picklist.getPicklistId()==0){
            //保存操作
            sysPicklistService.insertPicklist(picklist);
        }else{
            //编辑操作
            sysPicklistService.updatePicklist(picklist);
        }

        //删除配置的数值
        sysPicklistvalueService.deletebyPicklistId(picklist.getPicklistId());

        List<SysPicklistvalue> sysPicklistvalues = new ArrayList<>();
        picklistvalues = picklistvalues.replaceAll(" ","");
        String[] splits = picklistvalues.split("\r\n");
        for(int i=0;i<splits.length;i++){
            SysPicklistvalue picklistvalue = new SysPicklistvalue();
            picklistvalue.setPicklistId(picklist.getPicklistId());
            picklistvalue.setPvalue(splits[i].trim());
            picklistvalue.setSeq(i);
            sysPicklistvalues.add(picklistvalue);
        }

        //批量insert记录
        sysPicklistvalueService.insertPicklistValues(sysPicklistvalues);

        return JSON.toJSONString(new Message(ApplicationCode.SUCCESS.getCode(),ApplicationCode.SUCCESS.getMessage()));
    }

    @RequiresPermissions("picklist_delete")
    @RequestMapping("/remove.action")
    @ResponseBody
    public String remove(Integer picklist_id){
        if(sysPicklistService.removePicklist(picklist_id)>0){
            return JSON.toJSONString(new Message(ApplicationCode.SUCCESS.getCode(),ApplicationCode.SUCCESS.getMessage()));
        }else{
            return JSON.toJSONString(new Message(ApplicationCode.ERROR.getCode(),ApplicationCode.ERROR.getMessage()));
        }
    }

}
